home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0059_Bytes to Strings in BASM.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  1KB  |  37 lines

  1. {
  2. I needed a routine to convert a byte value into a string with leading zeros.
  3. > So I made one in BASM: Byte2lzStr. If you want, include this in SWAG.
  4. }
  5. var s: string;
  6.     tel, n : byte;
  7.  
  8. procedure Byte2lzStr( n, width: byte; var str: string ); assembler;
  9.   { Byte to string with leading zeros }
  10. asm
  11.         std                 { string operations backwards }
  12.         mov   al, [n]       { numeric value to convert    }
  13.         mov   cl, [width]   { width of str                }
  14.         xor   ch, ch        { clear ch                    }
  15.         les   di, str       { adress of str               }
  16.         mov   [di], cl      { length of str               }
  17.         add   di, cx        { start with last char str    }
  18. @start: jcxz  @exit         { done?                       }
  19.         aam                 { divide al by 10             }
  20.         add   al, 30h       { convert remainder to char   }
  21.         stosb               { store digit                 }
  22.         xchg  al, ah        { swap remainder and quotient }
  23.         dec   cl            { count down                  }
  24.         jmp   @start        { next digit                  }
  25. @exit:
  26. end  { Byte2lzStr };
  27.  
  28. begin
  29.   randomize;
  30.   for tel := 1 to 24 do
  31.   begin
  32.     n := random( 256 );
  33.     Byte2lzStr( n, 5, s );
  34.     writeln( tel:2,':  ', n:3,'  ', s,'  [',length(s),']' );
  35.   end;
  36. end.
  37.